﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using BlueCrest.Prism.Common.UI;
using BlueCrest.Prism.Common.UI.Commands;
using BlueCrest.Prism.Common.UI.Modules;
using BlueCrest.Prism.Common.UI.Ribbon;
using BlueCrest.Prism.Common.UI.Ribbon.Entities;
using BlueCrest.Prism.Common.UI.Services;
using BlueCrest.Prism.Modules.Common.Ribbon.ViewModels;
using BlueCrest.Prism.Modules.Common.Ribbon.Views;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace BlueCrest.Prism.Modules.Common.Ribbon
{
    public class RibbonModule : IModule
    {
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;

        public RibbonModule(IRegionManager regionManager, IUnityContainer container)
        {
            _regionManager = regionManager;
            _container = container;
        }

        public void Initialize()
        {
            new ContainerInitializer().Initialize(_container);

            ConfigureModuleRibbons();

            var mainRegion = _regionManager.Regions[RegionNames.MainRegion];
            var view = _container.Resolve<IRibbonView>();
            view.DataContext = _container.Resolve<IRibbonViewModel>();
            mainRegion.Add(view);
        }

        public void ConfigureModuleRibbons()
        {
            var ribbonEstate = new RibbonEstate();

            ribbonEstate.Tabs = new List<RibbonTabEntity>
                       {
                           new RibbonTabEntity("Ribbon Test")
                               .AddGroup(
                                    new RibbonGroupEntity("Ribbon Group A")
                                        .AddItem(new RibbonItem
                                                    {
                                                        Name = "Ribbon Item",
                                                        ItemType = RibbonItemType.Button,
                                                        Command = new DelegatingCommand(a => MessageBox.Show("Test A")),
                                                    })
                                        ),
                            //same tab
                            new RibbonTabEntity("Ribbon Test")
                               .AddGroup(
                                    new RibbonGroupEntity("Ribbon Group B")
                                        .AddItem(new RibbonItem
                                                    {
                                                        Name = "Ribbon Item B1",
                                                        ItemType = RibbonItemType.Button,
                                                        Command = new DelegatingCommand(a => MessageBox.Show("Test B1")),
                                                    })
                                        ),

                            new RibbonTabEntity("Ribbon Test")
                               .AddGroup(
                                    new RibbonGroupEntity("Ribbon Group B")
                                        .AddItem(new RibbonItem
                                                    {
                                                        Name = "Ribbon Item B2",
                                                        ItemType = RibbonItemType.Button,
                                                        Command = new DelegatingCommand(a => MessageBox.Show("Test B2")),
                                                    })
                                        )

                       };
            
            ribbonEstate.ApplicationMenu.Add(new RibbonItem
                                                 {
                                                     Name = "Exit",
                                                     ItemType = RibbonItemType.Button,
                                                     Command = new DelegatingCommand(a => MessageBox.Show("Exiting the app")),
                                                 });

            var ribbonService = _container.Resolve<IRibbonService>();

            ribbonService.AddTab(ribbonEstate);
        }


    }
}
